//
//  ITProgressViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/17.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITProgressViewController.h"
#import <JKCategories/JKCategories.h>

@interface ITProgressViewController ()

@property (nonatomic, strong) UIProgressView *progressView;

@end

@implementation ITProgressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupViews];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    __block int i = 1;
    if (@available(iOS 10.0, *)) {
        [NSTimer scheduledTimerWithTimeInterval:0.01 repeats:YES block:^(NSTimer * _Nonnull timer) {
            if (i == 101) {
                [timer invalidate];
            }
            [self.progressView setProgress:1.0 * i / 100 animated:YES];
            i ++;
        }];
    } else {
        // Fallback on earlier versions
    }
}

-(void)viewWillLayoutSubviews {
    self.progressView.frame = CGRectMake(50, 110, self.view.bounds.size.width - 100, 5);
    CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 5.f);
    self.progressView.transform = transform;
    self.progressView.layer.cornerRadius = 5;
    self.progressView.layer.masksToBounds = YES;
    [self.progressView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.layer.cornerRadius = 2.5;
        obj.layer.masksToBounds = YES;
    }];

}

-(void)setupViews {
    //1. 系统进度条 可调整圆角 高度 设置渐变色 基本满足所有场景
    self.progressView = [[UIProgressView alloc] init];
    UIColor *from = [UIColor jk_colorWithHex:0x00CCFF];
    UIColor *to = [UIColor jk_colorWithHex:0x009DFF];
    self.progressView.tintColor = [self wgk_gradientFromColor:from toColor:to withHeight:2];
//    self.progressView.tintColor = [self wgk_gradientFromColor:[UIColor redColor] toColor:[UIColor blueColor] withHeight:2];
    [self.view addSubview:self.progressView];
}

/**
 *  @brief  渐变颜色
 *
 *  @param c1     开始颜色
 *  @param c2     结束颜色
 *  @param height 渐变高度
 *
 *  @return 渐变颜色
 */
- (UIColor*)wgk_gradientFromColor:(UIColor*)c1 toColor:(UIColor*)c2 withHeight:(int)height
{
    return [UIColor jk_gradientFromColor:c1 toColor:c2 withHeight:height];
//    CGSize size = CGSizeMake(1, height);
//    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
//
//    NSArray* colors = [NSArray arrayWithObjects:(id)c1.CGColor, (id)c2.CGColor, nil];
//    CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
//    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);
//
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//
//    CGGradientRelease(gradient);
//    CGColorSpaceRelease(colorspace);
//    UIGraphicsEndImageContext();
//
//    return [UIColor colorWithPatternImage:image];
}

@end
